const xss = require('xss')

const { Comment } = require('../models/comment')
const { Reply } = require('../models/reply')

class CommentDao {
    // 创建评论
    static async create(v) {
        const comment = new Comment();
        comment.com_name = xss(v.get('body.com_name'));
        comment.com_avatar = xss(v.get('body.com_avatar'));
        comment.com_phone = xss(v.get('body.com_phone'));
        comment.content = xss(v.get('body.content'));
        comment.com_id = xss(v.get('body.com_id'));
        comment.com_type = xss(v.get('body.com_type'));

        comment.save();
        return {
            msg: '创建评论成功'
        }
    }

    // 删除评论
    static async destroy(id) {
        const comment = await Comment.findOne({
            where: {
                id,
                deleted_at: null
            }
        });
        if (!comment) {
            return {
                msg: '没有找到相关评论'
            }
        }
        comment.destroy()
        return {
            msg: '删除评论成功'
        }
    }

    // 获取评论详情
    static async detail(id) {
        const comment = await Comment.scope('iv').findOne({
            where: {
                id,
                deleted_at: null
            },
            attributes: {
                exclude: ['email', 'updated_at']
            },
            include: [{
                model: Reply,
                as: 'reply',
                attributes: {
                    exclude: ['email', 'updated_at', 'deleted_at']
                }
            }]
        });
        if (!comment) {
            return {
                msg: '没有找到相关评论信息'
            }
        }

        return {
            data: comment,
            msg: '获取评论信息成功'
        }
    }

    // 更新评论
    static async update(id, v) {
        const comment = await Comment.findByPk(id);
        if (!comment) {
            return {
                msg: '没有找到相关评论信息'
            }
        }
        comment.com_name = xss(v.get('body.com_name'));
        comment.com_avatar = xss(v.get('body.com_avatar'));
        comment.com_phone = xss(v.get('body.com_phone'));
        comment.content = xss(v.get('body.content'));
        comment.com_id = xss(v.get('body.com_id'));
        comment.com_type = xss(v.get('body.com_type'));

        comment.save();
        return {
            msg: '更新评论成功'
        }
    }


    // 评论列表
    static async list(params) {
        const { com_id , page = 1, pageSize = 10 } = params;

        // 筛选方式
        let filter = {
          deleted_at: null
        };
    
        // 筛选方式：存在分类
        if (com_id) {
          filter.com_id = Number(com_id);
        }
        const comment = await Comment.findAndCountAll({
            // 每页10条
            limit: pageSize,
            offset: (page - 1) * pageSize,
            where: filter,
            order: [
                ['created_at', 'DESC']
            ],
            attributes: {
                exclude: ['updated_at','deleted_at']
            },
        })

        return {
            msg: '获取评论列表成功',
            data: comment.rows,
            meta: {
                current_page: parseInt(page),
                per_page: 10,
                count: comment.count,
                total: comment.count,
                total_pages: Math.ceil(comment.count / 10),
            }
        };
    }

    // 关联目标下的评论
    static async CommentConnect(params = {}) {
        const {
            com_id,
            com_type,
            page = 1,
            desc = 'created_at'
        } = params;

        const pageSize = 10;
        const comment = await Comment.findAndCountAll({
            where: {
                com_type,
                com_id,
                deleted_at: null
            },
            // 每页10条
            limit: pageSize,
            offset: (page - 1) * pageSize,
            order: [
                [desc, 'DESC']
            ],
            attributes: {
                exclude: ['email', 'updated_at']
            },
            include: [{
                model: Reply,
                as: 'reply',
                attributes: {
                    exclude: ['email', 'updated_at', 'deleted_at']
                }
            }]
        })

        return {
            msg: '获取评论成功',
            data: comment.rows,
            // 分页
            meta: {
                current_page: parseInt(page),
                per_page: 10,
                count: comment.count,
                total: comment.count,
                total_pages: Math.ceil(comment.count / 10),
            }
        };
    }

    // 关联目标下的所有评论
    static async AllTargetComment(params = {}) {
        const { com_id, com_type } = params;

        const comment = await Comment.findAndCountAll({
            where: {
                com_type,
                com_id,
                deleted_at: null
            },
            order: [
                ['created_at', 'DESC']
            ],
            attributes: {
                exclude: ['email', 'updated_at']
            },
            include: [{
                model: Reply,
                as: 'reply',
                attributes: {
                    exclude: ['email', 'updated_at', 'deleted_at']
                }
            }]
        })
        return {
            msg: '获取所有评论成功',
            data: comment.rows,
            // 分页
            meta: {
                total: comment.count
            }
        };
    }
}

module.exports = {
    CommentDao
}
